package com.zyz.controller;

import com.github.pagehelper.PageInfo;
import com.zyz.common.ResultInfo;
import com.zyz.domain.Lesson;
import com.zyz.domain.User;
import com.zyz.handler.UserHolder;
import com.zyz.service.CoachService;
import com.zyz.service.CourseService;
import com.zyz.service.EquipmentService;
import com.zyz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private CoachService coachService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private EquipmentService equipmentService;

    //查询用户信息
    @GetMapping("/user/queryUserInfo")
    public ResultInfo findAll(){
        User user1 = UserHolder.get();
        Integer id = user1.getId();
        User user=userService.findById(id);
        return ResultInfo.success(user);
    }
    //用户信息更新
    @PostMapping("/user/updateUserInfo")
    public ResultInfo update(@RequestBody User user){
        userService.update(user);
        return ResultInfo.success(null);
    }
    //查询用户课程
    @GetMapping("/user/queryUserTask")
    public ResultInfo findLessonId(){
       Lesson lesson=userService.findLessinId();
        return ResultInfo.success(lesson);
    }
    //管理员更新用户总数
    @GetMapping("/user/queryUserTotal")
    public ResultInfo getTotal(){
        Integer num=userService.getTotal();
        return ResultInfo.success(num);
    }
    @GetMapping("/coach/queryCoachTotal")
    public ResultInfo getCoachTotal(){
        Integer num=coachService.getCoachTotal();
        return ResultInfo.success(num);
    }
    @GetMapping("/course/queryCourseTotal")
    public ResultInfo getLessonTotal(){
        Integer num=courseService.getLessonTotal();
        return ResultInfo.success(num);
    }
    @GetMapping("/equipment/queryEquipmentTotal")
    public ResultInfo getEquipmentTotal(){
       Integer num= equipmentService.getEqTotal();
       return ResultInfo.success(num);
    }
    @PostMapping("/user/findAllUsers")
    public ResultInfo findVip(@RequestParam(value = "page",defaultValue = "1")Integer pageNum,
                              @RequestParam(value = "pageSize") Integer pageSize){
        PageInfo<User> page=userService.findVip(pageNum,pageSize);
        return ResultInfo.success(page);
    }
    //修改回显
    @PostMapping("/user/queryUserToUpdate")
    public ResultInfo findMessage(Integer id){
        User user=userService.findMessage(id);
        return ResultInfo.success(user);
    }
    @PostMapping("/user/updateUser")
    public ResultInfo updateUser(@RequestBody User user){
        userService.updateUser(user);
        return ResultInfo.success(null);
    }
    //删除
    @PostMapping("/user/deleteUserById")
    public ResultInfo deleteUser(Integer id){
        userService.deleteUser(id);
        return ResultInfo.success(null);
    }
    //添加
    @PostMapping("/user/addNewUser")
    public ResultInfo addNewUser(@RequestBody User user){
        userService.insertUser(user);
        return ResultInfo.success(null);
    }
}
